#include <stdio.h>
#include <stdlib.h>

void replace(char *s, char ch);

int main(int argc, char *argv[])
{
    if(argc < 2) return 0;
    replace(argv[1], '-');
    printf("%s\n", argv[1]);

    return 0;
}

void replace(char *s, char ch)
{
    int first = -1, last = 0, strLen = 0, i, j;
    char *tmp;
    if(s == NULL) return;

    while(s[strLen] != '\0')
    {
        if(s[strLen] != ch)
        {
            last = strLen;
            if(first == -1)
                first = strLen;
        }
        strLen++;
    }
    if(first == -1 || first == last) return;
    printf("firt: %d, last: %d\n", first, last);
    // one more for '\0'
    tmp = malloc(sizeof(char) * (strLen + 1));
    for(i = 0, j = 0; i < strLen; i++)
    {
        if(i <= first || i >= last || s[i] != ch)
        {
            tmp[j] = s[i];
            j++;
            continue;
        }
        if(tmp[j - 1] != ch)
        {
            tmp[j] = s[i];
            j++;
            continue;
        }
    }
    tmp[j] = '\0';
    for(i = 0; i <= j; i++)
    {
        s[i] = tmp[i];
    }
    free(tmp);
}
